package cn.edu.bupt.freewings.forum.client.rest;

import cn.edu.bupt.freewings.forum.common.UserType;
import cn.edu.bupt.freewings.forum.common.CommonType;
import cn.edu.bupt.freewings.forum.client.Constants;
import cn.edu.bupt.freewings.forum.client.base.ClientUtility;

import org.apache.log4j.Logger;

public class UserClient {
	Logger logger = Logger.getLogger(UserClient.class.getName());

	public UserType.User getUserById(Integer id) {
		return (UserType.User) ClientUtility.makeHttpRequest(
				ClientUtility.HTTPMethod.GET, Constants.REST_SERVER + "/user/get?id="
						+ id, null, null, "application/proto", UserType.User.class);
	}

	public UserType.User getByName(String name) {
		logger.info("get id");
		return (UserType.User) ClientUtility.makeHttpRequest(
				ClientUtility.HTTPMethod.GET, Constants.REST_SERVER + "/user/get?name="
						+ name, null, null, "application/proto", UserType.User.class);
	}

	public boolean put(UserType.User user) {
		return ((CommonType.Boolean) ClientUtility.makeHttpRequest(
				ClientUtility.HTTPMethod.PUT, Constants.REST_SERVER + Constants.USER
						+ "/new", Constants.MIME_PROTO, user.toByteArray(),
				Constants.MIME_PROTO, CommonType.Boolean.class)).getValue();
	}

	public static void main(String args[]) {
		System.out.println(new UserClient().getByName("root"));
	}
}
